// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import VueRouter from 'vue-router';
import { sync } from 'vuex-router-sync';

import './common/less/index.less';
import Utils from './common/js/utils';

Vue.use(Utils);

import FastClick from 'fastclick';
FastClick.attach(document.body);
import routes from './routes';
import store from './store';

// import VueScroller from 'vue-scroller';
// Vue.use(VueScroller);

Vue.use(VueRouter);
// const routes = [];

require('es6-promise').polyfill();

const router = new VueRouter({
    // mode: 'history',
    // base: __dirname,
    routes
    // linkActiveClass: 'active'
});

// plugins
import { LocalePlugin, DevicePlugin, ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin, WechatPlugin, AjaxPlugin, AppPlugin } from 'vux';
Vue.use(DevicePlugin);
Vue.use(ToastPlugin);
Vue.use(AlertPlugin);
Vue.use(ConfirmPlugin);
Vue.use(LoadingPlugin);
Vue.use(WechatPlugin);
Vue.use(AjaxPlugin);
Vue.use(LocalePlugin);

// test
if (process.env.platform === 'app') {
    // console.log(process.env);
    Vue.use(AppPlugin, store);
}

// rem自适应
(function (doc, win) {
    var docEl = doc.documentElement;
    var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
    var recalc = function () {
    var clientWidth = docEl.clientWidth;
    if (!clientWidth) return;
    if (clientWidth > 640) clientWidth = 640;
    docEl.style.fontSize = clientWidth / 7.5 + 'px';
    };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

import API from './api/API';
let api = new API();

window.api = api;

// function ismobile () {
//   let u = navigator.userAgent;
//   if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
//     return 1;
//   } else if (u.indexOf('iPhone') > -1) {
//     return 0;
//   } else if (u.indexOf('Windows Phone') > -1) {
//     return -1;
//   }
// }

// 初始化登录状态
// store.commit('updateIsLogin');

// 检测手机类型
// store.commit('updatePhoneType', {phoneType: ismobile()});

sync(store, router);

/* 切换 */
const history = window.sessionStorage;
history.clear();
let historyCount = history.getItem('count') * 1 || 0;
history.setItem('/', 0);

router.beforeEach(function (to, from, next) {
    // debugger;
    store.commit('updateLoadingStatus', {isLoading: true});

    const toIndex = history.getItem(to.path);
    const fromIndex = history.getItem(from.path);

    if (toIndex) {
        if (toIndex > fromIndex || !fromIndex || (toIndex === '0' && fromIndex === '0')) {
            store.commit('updateDirection', {direction: 'forward'});
        } else {
            store.commit('updateDirection', {direction: 'reverse'});
        }
    } else {
        ++historyCount;
        history.setItem('count', historyCount);
        to.path !== '/' && history.setItem(to.path, historyCount);
        store.commit('updateDirection', {direction: 'forward'});
    }

    if (/\/http/.test(to.path)) {
        let url = to.path.split('http')[1];
        window.location.href = `http${url}`;
    } else {
        next();
    }
});

router.afterEach(function (to) {
    setTimeout(() => {
        store.commit('updateLoadingStatus', {isLoading: false});
    }, 1000);
    // if (0 && process.env.NODE_ENV === 'production') {
    //   ga && ga('set', 'page', to.fullPath);
    //   ga && ga('send', 'pageview');
    // }
});

VueRouter.prototype.goBack = function () {
    // debugger;
    this.isBack = true;
    window.history.go(-1);
};

/* 指令 */
// Vue.directive('drag', function (el, binding) {
//   el.style.backgroundColor = binding.value;
// });

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    render: h => h(App)
});
